<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\TongjiService;
use App\Service\Admin\WhywService;
use Illuminate\Http\Request;

class WhywController extends BaseController
{
    //工时录入

    //列表
    public function index(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['uid'] = $request->post('uid', 0);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['sort_id'] = $request->post('sort_id', 1);
        $res = WhywService::index($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['uid'] = $request->post('uid', 0);
        $res = WhywService::getOne($id, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //任务列表
    public function getTaskList(Request $request)
    {
        $in_day = $request->post('in_day', '');
        $depart_id = $request->post('depart_id', 0);
        $res = WhywService::getTaskList($in_day, $depart_id);
        if (empty($res)) {
            return self::toJson(-2, '没有数据');
        }
        return self::toJson(0, '', $res);
    }

    //保存
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['in_day'] = $request->post('in_day', '');
        $pas['item_id'] = $request->post('item_id', 0);
        $pas['task_id'] = $request->post('task_id', 0);
        $res = WhywService::modify($id, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //发布
    public function pub(Request $request)
    {
        $id = $request->post('id', 0);
        $res = WhywService::pub($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //设置项目
    public function setByItem(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['staff_id'] = $request->post('uid', 0);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['item_id'] = $request->post('item_id', 0);
        $pas['task_id'] = $request->post('task_id', 0);
        $pas['in_day'] = $request->post('in_day', '');
        if (!$pas['item_id']) {
            return self::toJson(-2, '项目必须的');
        }
        if (!$pas['in_day']) {
            return self::toJson(-2, '日期必须的');
        }
        $res = WhywService::setByItem($id, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = WhywService::del($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //审核列表
    public function authIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['key'] = $request->post('kw', '');
        $pas['sort_id'] = $request->post('sort_id', 1);
        if (!in_array($pas['role_id'], [1,2,3,5])) {
            return self::toJson(-2, '必须是审核员或者主管');
        }
        $res = WhywService::authIndex($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //已审核列表
    public function okIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['key'] = $request->post('kw', '');
        $pas['sort_id'] = $request->post('sort_id', 1);
        if (!in_array($pas['role_id'], [1,2,3,5])) {
            return self::toJson(-2, '必须是审核员或者主管');
        }
        $res = WhywService::okIndex($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //审核通过
    public function authOk(Request $request)
    {
        $id = $request->post('id', 0);
        $res = WhywService::authOk($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //审核通过
    public function authNo(Request $request)
    {
        $id = $request->post('id', 0);
        $remark = $request->post('remark', '');
        $res = WhywService::authNo($id, $remark);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //设置为默认
    public function itemDefault(Request $request)
    {
        $uid = $request->post('uid', 0);
        $item_id = $request->post('item_id', 0);
        $res = WhywService::itemDefault($uid, $item_id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //拒绝详情
    public function getRefuseOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = WhywService::getRefuseOne($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }
}